<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class BankCard
 * 
 * @property int $id
 * @property int|null $bank_id
 * @property string|null $bank_num
 * @property int|null $user_id
 * @property string|null $bank_sum_name
 * @property string|null $account_name
 * @property string|null $phone_num
 * @property int|null $is_default
 * @property int|null $id_type
 * @property int|null $old_id
 * @property int|null $status
 * @property int|null $tl_auth_id
 * @property Carbon|null $update_time
 * @property Carbon|null $create_time
 * 
 * @property Bank|null $bank
 * @property TlAuth|null $tl_auth
 * @property User|null $user
 * @property Collection|Image[] $images
 * @property Collection|MassageChangeRecord[] $massage_change_records
 * @property Collection|Order[] $orders
 * @property Collection|TlAuth[] $tl_auths
 *
 * @package App\Models
 */
class BankCard extends Model
{
    protected $table = 'bank_card';

    protected $casts = [
        'bank_id' => 'int',
        'user_id' => 'int',
        'is_default' => 'int',
        'id_type' => 'int',
        'old_id' => 'int',
        'status' => 'int',
        'tl_auth_id' => 'int'
    ];

    protected $fillable = [
        'bank_id',
        'bank_num',
        'user_id',
        'bank_sum_name',
        'account_name',
        'phone_num',
        'is_default',
        'id_type',
        'old_id',
        'status',
        'tl_auth_id',
        'sign_num',
    ];
    protected $appends = [
        'bank_name',/*银行名称*/
        'bank_obverse_images',/*银行卡正面*/
        'bank_verso_images',/*银行卡反面*/
    ];
    public function bank()
    {
        return $this->belongsTo(Bank::class);
    }

    public function tl_auth()
    {
        return $this->belongsTo(TlAuth::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function images()
    {
        return $this->hasMany(Image::class);
    }

    public function massage_change_records()
    {
        return $this->hasMany(MassageChangeRecord::class, 'old_bank_card_id');
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    public function tl_auths()
    {
        return $this->hasMany(TlAuth::class);
    }
    /**
     * @description: 银行名称
     * @param {*}
     * @return string
     */
    public function getBankNameAttribute()
    {
        return $this->bank->bank_name ?? '';
    }
    /**
     * @description:  bank_obverse_images 银行卡正面
     * @param {*}
     * @return array
     */
    public function getBankObverseImagesAttribute()
    {
        return $this->images->where('img_type', '5')->all();
    }
    /**
     * @description: bank_verso_images 银行卡反面
     * @param {*}
     * @return array
     */
    public function getBankVersoImagesAttribute()
    {
        return $this->images->where('img_type', '6')->all();
    }
}
